using IM.Easy.Infrastructure.Query;
using Microsoft.AspNetCore.Mvc;
using IM.Easy.SystemServices.Dtos;
using IM.Easy.SystemServices.IServices;
using IM.Easy.Entity.System;
using System.IO;
using IM.Easy.Core.Attributes;
using IM.Easy.Core.Utils;

namespace IM.Easy.Api.Controllers.System
{
    /// <summary>
    /// FolderController 控制器
    /// </summary>
    [Route("system/folder")]
    public class FolderController : ControllerBase
    {
        private readonly IFolderService _service;
        private readonly IFileResourceService _fileResourceService;

        /// <summary>
        /// 构造函数，注入服务
        /// </summary>
        /// <param name="service">服务接口</param>
        public FolderController(IFolderService service, IFileResourceService fileResourceService)
        {
            _service = service;
            _fileResourceService = fileResourceService;
        }

        /// <summary>
        /// 获取文件夹下面的所有内容
        /// </summary>
        /// <returns></returns>
        [Api(Name = "获取文件夹内容", Perm = "folder:contents")]
        [HttpGet("contents")]
        public ResourcesDto GetContents([FromQuery] FolderQueryDto query)
        {
            var resource = _service.GetContents(query);
            var contents = resource.Content;
            foreach (var item in contents)
            {
                if (!item.IsFolder)
                    item.Url = _fileResourceService.MapToNetworkUrl(item.PhysicalPath);
            }
            return resource;
        }

        /// <summary>
        /// 获取子内容
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [Api(Name = "查询文件夹内容", Perm = "folder:contents")]
        [HttpGet("subcontents")]
        public List<ResourcesDto> GetSubContents([FromQuery] FolderQueryDto query)
        {
            if (string.IsNullOrEmpty(query.Keyword)) ExceptionUtils.ThrowArgumentException(nameof(query.Keyword), "请填写参数");
            var resources = _service.GetSubContents(query);
            return resources;
        }

        /// <summary>
        /// 根据ID获取Folder对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        /// <returns>返回指定的Folder对象</returns>
        [Api(Name = "获取文件夹详情", Perm = "folder:info")]
        [HttpGet("{id}")]
        public Folder GetById([FromRoute] Guid id)
        {
            var result = _service.GetById(id);
            return result ?? throw new KeyNotFoundException($"未找到ID为 {id} 的Folder对象。");
        }

        /// <summary>
        /// 创建新的Folder对象
        /// </summary>
        /// <param name="entity">要创建的Folder对象</param>
        /// <returns>返回创建后的Folder对象</returns>
        [Api(Name = "创建文件夹", Perm = "folder:add")]
        [HttpPost]
        public Folder Create([FromBody] Folder entity)
        {
            _service.Create(entity);
            return entity; // 直接返回创建的实体
        }

        /// <summary>
        /// 批量删除Folder对象
        /// </summary>
        /// <param name="ids">对象的IDS</param>
        [Api(Name = "递归删除文件夹", Perm = "folder:del")]
        [HttpDelete]
        public void Delete([FromBody] Guid[] ids)
        {
            var resources = _service.GetSubContents(ids);
            var files = resources.Where(s => !s.IsFolder).ToList();
            var folders = resources.Where(s => s.IsFolder).ToList();

            if (folders != null && folders.Count > 0)
            {
                var fids = folders.Select(s => s.Id.Value).ToArray();
                _service.Delete(fids);
            }

            if (files != null & files.Count > 0)
            {
                var fids = files.Select(s => s.Id.Value).ToArray();
                _fileResourceService.Delete(fids);
                var physicalPaths = files.Select(s => s.PhysicalPath).ToArray();
                _fileResourceService.DeleteStorage(physicalPaths);
            }
        }

        /// <summary>
        /// 文件夹重命名
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPut("rename")]
        [Api(Name = "文件夹重命名", Perm = "folder:put")]
        public bool Rename([FromBody] ResourcesDto resource)
        {
            return _service.Rename(resource.Id.Value, resource.Name);
        }
    }
}
